package com.mcxx.util;

import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.component.message.send.util.MessageSendUtil;
import com.yinhai.ta404.component.message.vo.NoticeVo;
import com.yinhai.ta404.core.utils.ResponseExportUtil;
import com.yinhai.ta404.core.utils.ZipUtil;
import org.apache.commons.lang3.RandomUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

public class ZipInfoUtil {

    public static void exportZipResponse(HttpServletResponse response, ExcelInfo excelInfo, String userId) throws IOException {
        String password = generatePassword();
        File zipFile = ZipUtil.compressFileForObject(excelInfo.getFileName(), excelInfo.getFile());
        ResponseExportUtil.exportFile(response, zipFile);
        zipFile.delete();

    }

    public static void exportZipByte(HttpServletResponse response, String name,byte[] file, String userId) throws IOException {
        String password = generatePassword();
        File zipFile = ZipUtil.compressFileForObject(name, file);
        ResponseExportUtil.exportFile(response, zipFile);
        zipFile.delete();

    }


    public static void exportZipByte(HttpServletResponse response, String name,byte[] file) throws IOException {
        File zipFile = ZipUtil.compressFileForObject(name, file);
        ResponseExportUtil.exportFile(response, zipFile);
        zipFile.delete();
    }
    public static void noticeZipInfoToControlUser(String zipFileName, String userId, String password) {
        StringBuffer content = new StringBuffer();
        content.append("<p>");
        content.append("文件名:&nbsp;");
        content.append(zipFileName);
        content.append("</p>");
        content.append("<p>");
        content.append("密码:&nbsp;");
        content.append(password);
        content.append("</p>");
        NoticeVo noticeVo = new NoticeVo();
        noticeVo.setTitle("文件密码告知");
        noticeVo.setContent(content.toString());
        MessageSendUtil.sendNotice(userId, noticeVo);
    }

    public static String generatePassword() {
        int xs = (int)Math.pow(10.0D, 6.0D);
        int start = xs / 10;
        int end = xs - 1;
        return String.valueOf(RandomUtils.nextLong((long)start, (long)end));
    }
}
